<template>
	<view class="address">

		<view class="list">
			<block v-for="item, index in address_list">
				<view class="item">

					<view v-on:click="checkAddr(item)" class="name-phone">
						<text class="name">{{item.name}}</text>,
						<text class="phone">{{item.telephone}}</text>
					</view>

					<view v-on:click="checkAddr(item)" class="detail">{{item.detail_address}}</view>
					<view class="edit">
						<navigator v-bind:url="'/sm_shop/pages/address-edit/address-edit?id='+item.id">
							<image mode="widthFix" src="/static/image/skin/edit.png"></image>
						</navigator>
					</view>
				</view>
			</block>


		</view>

		<view class="add-button">
			<!-- #ifdef MP-WEIXIN -->
			<u-button class="bottom-btn" v-on:click="getWxAddress" >
				获取微信地址
			</u-button>
			<!-- #endif -->
			<u-button type="primary" class="bottom-btn" v-on:click="addAddress">
				<navigator url="/sm_shop/pages/address-edit/address-edit">
					新增收货地址
				</navigator>
			</u-button>
			
			

		</view>
	</view>
</template>

<script>
	const appInstance = getApp();
	export default {
		data() {
			return {
				origin: '',
				address_list: []
			}
		},
		onLoad: function(options) {
			if (options.origin) {
				this.origin = options.origin
			}
			this.init();
		},
		onShow:function(){
			this.init();
		},
		methods: {
			init: function() {
				var t = this;
				var url = 'r=address.get_list';
				//todo 获取收货地址列表
				appInstance.globalData.request(url, {}, 'GET', res => {

					t.address_list = res.data

				});
			},

			checkAddr: function( item ) {

				// console.log(e.currentTarget.dataset.item);

				if (this.origin == 'order-create') {
					//todo 选中地址 并返回订单创建页面

					uni.setStorage({
						key: 'order-address',
						data: item
					});

					uni.navigateBack();


				}


			},
			
			// 微信地址
			getWxAddress:function(  ){
				var t = this;
				wx.chooseAddress({
				  success (res) {
				    console.log(res)				   
					t.saveWxAddress( res );
				  }
				})
			},
			
			addAddress: function( res ) {
				uni.navigateTo({
					url:'/sm_shop/pages/address-edit/address-edit'
				});
			},
			saveWxAddress: function( res ) {
			
				var t = this;
				var url ='&r=address.edit';
			
				var data = {
					name : res.userName,
					telephone : res.telNumber,
					detail_address : res.detailInfo,
					province_id : 0,
					province_name : res.provinceName,
					city_id : 0,
					city_name : res.cityName,
					area_id : 0,
					area_name : res.countyName,
					is_default : 0,
				};
			
				appInstance.globalData.request(url, data, "POST", res => {
			
					if (res.data.status == 0) {
						// 刷新列表
						t.init();
					}
			
				});
			
			},
		}
	}
</script>

<style lang="less">
	@import './address.less';
</style>
